package com.dong.rabbitmq.config;

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * @author dongjian
 * TTL队列 配置文件类
 */
@Configuration
public class TtlQueueConfig {

    /**
     * 普通交换机名称
     */
    public static final String X_EXCHANGE = "X";
    /**
     * 死信交换机名称
     */
    public static final String DEAD_LETTER_EXCHANGE = "Y";
    /**
     * 普通队列QA名称
     */
    public static final String QUEUE_A = "QA";
    /**
     * 普通队列QB名称
     */
    public static final String QUEUE_B = "QB";
    /**
     * 死信队列QD名称
     */
    public static final String DEAD_QUEUE = "QD";

    /**
     * 普通队列QCM名称
     */
    public static final String QUEUE_C = "QC";

    /**
     * 声明普通交换机 别名
     */
    @Bean("xExchange")
    public DirectExchange xExchange() {
        return new DirectExchange(X_EXCHANGE);
    }

    /**
     * 声明死信交换机
     */
    @Bean("deadLetterExchange")
    public DirectExchange deadLetterExchange() {
        return new DirectExchange(DEAD_LETTER_EXCHANGE);
    }

    /**
     * 声明普通队列QA
     */
    @Bean("aQueue")
    public Queue aQueue() {
        Map<String, Object> arguments = new HashMap<>();
        // 设置死信队列
        arguments.put("x-dead-letter-exchange", DEAD_LETTER_EXCHANGE);
        // 设置过期时间 单位ms
        arguments.put("x-message-ttl", 10000);
        // 设置routingKey
        arguments.put("x-dead-letter-routing-key", "YD");
        return QueueBuilder.durable(QUEUE_A).withArguments(arguments).build();
    }

    /**
     * 声明普通队列QB
     */
    @Bean("bQueue")
    public Queue bQueue() {
        Map<String, Object> arguments = new HashMap<>();
        // 设置死信队列
        arguments.put("x-dead-letter-exchange", DEAD_LETTER_EXCHANGE);
        // 设置过期时间 单位ms
        arguments.put("x-message-ttl", 40000);
        // 设置routingKey
        arguments.put("x-dead-letter-routing-key", "YD");
        return QueueBuilder.durable(QUEUE_B).withArguments(arguments).build();
    }

    /**
     * 声明普通队列QC
     * 不设置TTL时间，由生产者根据需求进行设置，更加灵活，可操作性更好
     */
    @Bean("cQueue")
    public Queue cQueue() {
        Map<String, Object> arguments = new HashMap<>(3);
        // 设置死信交换机
        arguments.put("x-dead-letter-exchange", DEAD_LETTER_EXCHANGE);
        // 设置死信队列routingKey
        arguments.put("x-dead-letter-routing-key", "YD");
        return QueueBuilder.durable(QUEUE_C).withArguments(arguments).build();
    }

    /**
     * 声明死信队列QD
     */
    @Bean("deadQueue")
    public Queue deadQueue() {
        return QueueBuilder.durable(DEAD_QUEUE).build();
    }

    // -------------------------绑定----------------------------------

    @Bean
    public Binding queueABindingX(@Qualifier("aQueue") Queue aQueue,
                                  @Qualifier("xExchange") DirectExchange xExchange) {
        return BindingBuilder.bind(aQueue).to(xExchange).with("XA");
    }

    @Bean
    public Binding queueBBindingX(@Qualifier("bQueue") Queue bQueue,
                                  @Qualifier("xExchange") DirectExchange xExchange) {
        return BindingBuilder.bind(bQueue).to(xExchange).with("XB");
    }

    @Bean
    public Binding queueDBindingY(@Qualifier("deadQueue") Queue deadQueue,
                                  @Qualifier("deadLetterExchange") DirectExchange deadLetterExchange) {
        return BindingBuilder.bind(deadQueue).to(deadLetterExchange).with("YD");
    }

    @Bean
    public Binding queueCBindingX(@Qualifier("cQueue") Queue cQueue,
                                  @Qualifier("xExchange") DirectExchange xExchange) {
        return BindingBuilder.bind(cQueue).to(xExchange).with("XC");
    }
}
